/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.patient.medicationtask;

import be.ugent.medirem.logic.MeasurementRemote;
import be.ugent.medirem.logic.PatientRemote;
import be.ugent.medirem.logic.TaskRemote;
import be.ugent.medirem.persistence.entity.details.MeasurementDetails;
import be.ugent.medirem.persistence.entity.details.MedicationTaskDetails;
import be.ugent.medirem.persistence.util.TaskStatus;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Roelie
 */
public class TaskUpdateServlet extends HttpServlet {

    @EJB
    private MeasurementRemote measurementBean;
    @EJB
    private PatientRemote patientBean;
    @EJB
    private TaskRemote taskBean;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getUserPrincipal().getName();
        Long pId = patientBean.getPatientId(username);
        Long taskId = Long.parseLong(request.getParameter("id"));
        String status = request.getParameter("status");

        boolean validate = true;

        if (taskId == null || status == null || status.isEmpty()) {
            validate = false;
        } else {
            try {
                if (status.equals("forgotten")) {
                    taskBean.updateTaskStatus(taskId, pId, TaskStatus.FORGOTTEN);
                    request.setAttribute("type", "forgotten");
                    request.getRequestDispatcher("/patient/medicationtask/confirmNotTaken.jsp").forward(request, response);
                } else if (status.equals("ignored")) {
                    taskBean.updateTaskStatus(taskId, pId, TaskStatus.IGNORED);
                    request.setAttribute("type", "ignored");
                    request.getRequestDispatcher("/patient/medicationtask/confirmNotTaken.jsp").forward(request, response);
                } else if (status.equals("take")) {
                    MedicationTaskDetails tDetails = taskBean.getTaskDetails(taskId);
                    request.setAttribute("task", tDetails);

                    String measurementType = tDetails.getPrescription().getMedication().getMeasurementType();
                    List<MeasurementDetails> measurements = measurementBean.getMeasurements(pId, measurementType);
                    request.setAttribute("measurements", measurements);

                    String unit = measurementBean.getMeasurementTypeUnit(measurementType);
                    request.setAttribute("unit", unit);

                    request.getRequestDispatcher("/patient/medicationtask/take.jsp").forward(request, response);
                }
            } catch (Exception ex) {
                validate = false;
            }
        }

        if (!validate) {
            request.getSession().setAttribute("flash", "An error has occured when trying to confirm your task");
            request.getRequestDispatcher("/patient/main.jsp").forward(request, response);
        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
